package com.clkj.zhonghui.controller.banner;


import com.clkj.zhonghui.pojo.banner.Banner;
import com.clkj.zhonghui.pojo.common.OSSUtil;
import com.clkj.zhonghui.pojo.common.Result;
import com.clkj.zhonghui.pojo.common.StatusCode;
import com.clkj.zhonghui.service.banner.BannerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author：周佳润
 * @Date：2024/7/10 上午9:35
 */
@RestController
@RequestMapping(value = "/banner")
@CrossOrigin
@Api(value = "/banner", tags = {"中汇轮播图"})
public class BannerController {

    @Autowired
    private BannerService bannerService;


    @PostMapping("/getBannerList")
    @ApiOperation(value = "获取轮播图列表")
    public Result getBannerList(@ApiParam(name = "page", value = "页码") @RequestParam("page") Integer page,
                                @ApiParam(name = "size", value = "条数") @RequestParam("size")Integer size,
                                @ApiParam(name = "bannerFlag", value = "类型0为禁用，1为启用") @RequestParam("bannerFlag")Integer bannerFlag
                                /*@ApiParam(name = "shopId", value = "门店id") @RequestParam("shopId")Integer shopId*/){
        try {
            PageInfo bannerList = bannerService.listBanners(page,size,bannerFlag);
            return new Result(true, StatusCode.OK, "查询成功",bannerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }
    @PostMapping("/addBanner")
    @ApiOperation(value = "添加轮播图")
    public Result addBanner(Banner banner,
                            @ApiParam(name = "flag", value = "添加 add，修改 edit") @RequestParam("flag") String  flag,
                            HttpServletRequest request){

        try {
            MultipartFile file = ((MultipartRequest) request).getFile("file");
            String fileName = "";
            if (file != null) {
                fileName = OSSUtil.uploadingImg(file,"zhonghui/banner");
//                fileName = FileUploadTool.upload(request, file, "mainPic", "D:\\nginx-1.19.1\\html\\zhibanzhang\\resource");
            }
            bannerService.addBanner(banner,flag,fileName);
            return new Result(true, StatusCode.OK, "添加成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加失败");
        }
    }
    @PostMapping("/deleteBanner")
    @ApiOperation(value = "删除轮播图")
    public Result deleteBanner(@ApiParam(name = "ids", value = "轮播id集合字符串") @RequestParam("ids") String ids){

        try {
            bannerService.deleteBanner(ids);
            return new Result(true, StatusCode.OK, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "删除失败");
        }
    }

    @PostMapping("/updateBanner")
    @ApiOperation(value = "更新轮播状态")
    public Result updateBanner(@ApiParam(name = "ids", value = "轮播id集合字符串") @RequestParam("ids") String ids,
                                @ApiParam(name = "bannerStatus", value = "状态") @RequestParam("bannerStatus") Integer bannerStatus){

        try {
            bannerService.updateBanner(ids,bannerStatus);
            return new Result(true, StatusCode.OK, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "更新失败");
        }
    }
}
